package com.sunline.poc.oozie.controller;

import com.sunline.poc.oozie.client.OozieDTO;
import com.sunline.poc.oozie.config.ApplicationException;
import com.sunline.poc.oozie.config.LoginUserContext;
import com.sunline.poc.oozie.service.OozieService;
import com.sunline.poc.oozie.service.UserService;
import com.sunline.poc.oozie.vo.UserInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/oozie")
public class OozieCtrl {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private  OozieService oozieService;

    @Autowired
    private UserService userService;

    @RequestMapping("/start")
    @ResponseBody
    public OozieDTO start(String userName,String passWord,String jobName) throws ApplicationException {
            if(StringUtils.isEmpty(userName) || StringUtils.isEmpty(passWord) || StringUtils.isEmpty(jobName)){
                throw  new RuntimeException("userName,passWord,jobName can not be null");
            }
            UserInfoVO userInfoVO = userService.findUserByUsername(userName,passWord);

            if (userInfoVO == null){
                throw new RuntimeException("user not found");
            }

            LoginUserContext.setLoginUser(userInfoVO);
            logger.info("login success, username={},password={},jobname={}",userName,passWord,jobName);
            OozieDTO dto = oozieService.start(jobName);
            logger.info("start succes,job id= {}",dto.getJobId());

            return  dto;
    }

    @RequestMapping("/kill")
    @ResponseBody
    public String kill(String userName,String passWord,String jobId){
        try {
            UserInfoVO userInfoVO = userService.findUserByUsername(userName,passWord);

            if (userInfoVO == null){
                throw new RuntimeException("user not found");
            }

            LoginUserContext.setLoginUser(userInfoVO);
            Boolean flag= oozieService.kill(jobId);
            logger.info("kill finish ,success flag = {}",flag.toString());
            if (flag){
                return  "kill success!";
            }else {
                return  "kill failed, see system logs";
            }


        }catch (Exception e){
            e.printStackTrace();
            throw  new RuntimeException(e.getMessage());
        }
    }

    @RequestMapping("/status")
    @ResponseBody
    public String status(String userName,String passWord,String jobId){
        UserInfoVO userInfoVO = userService.findUserByUsername(userName,passWord);

        if (userInfoVO == null){
            throw new RuntimeException("user not found");
        }

        LoginUserContext.setLoginUser(userInfoVO);

        return oozieService.getJobStatus(jobId);
    }


    @RequestMapping("/rerun")
    @ResponseBody
    public OozieDTO rerun(String userName,String passWord,String jobName,String etlDate) throws ApplicationException {
        UserInfoVO userInfoVO = userService.findUserByUsername(userName,passWord);

        if (userInfoVO == null){
            throw new RuntimeException("user not found");
        }

        LoginUserContext.setLoginUser(userInfoVO);
        return  oozieService.rerun(jobName,etlDate);
    }
}
